Table of Contents

Prologue

Problem Solving Techniques

I Problems

1 Searching

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.
.

.
.

.
.

.
.

.
.

.
.

.
.

.
.

. . .

. . .

. . . . .

. . . . . . .

.
Computing square roots .
.
1.1
Search a sorted array for k .
.
1.2
Search a sorted array for the ﬁrst element larger than k . . . .
1.3
Search a sorted array for A[i] = i
.
1.4
1.5
Search an array of unknown length .
1.6 Missing element, limited resources .
.
.
Intersect two sorted arrays .
1.7
.
Anagrams
1.8
.
.
.
.
Search for a pair which sums to S .
.
1.9
.
.
1.10 Anonymous letter .
.
.
1.11
.
.
1.12 Missing element .
.
.
.
1.13 Robot battery capacity .
.
.
1.14
.
.
.
1.15
.
.
1.16
.
.
1.17
.
.
1.18
.
.
1.19
.
.
.
1.20 Contained intervals .
.
.
.
1.21 View from the top .
.
.
1.22 Completion search .
.
.
1.23 Matrix search .
.
.
.
1.24 Checking simplicity .

. . . . . . . . . . . . . . . .
. . . .
. . . .
.
. . . .
.
. . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.
. . . .
. . . . . . .
.
. . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.
. . . . . . . . . . .
. . .
.
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
.

.
.
.
.
.
.
Search for majority .
.
.
Search for frequent items .
.
.
Search BST for a key .
.
Search BST for x > k .
.
.
Searching two sorted arrays .
.
Intersecting lines
.
.
.
.
.

.
Pairing users by attributes .
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

. . .

.
.

.
.

.
.

.

.

.

.

.

.

.

.

iii

1

5

13

14
15
16
16
16
16
16
16
17
17
18
18
18
18
19
19
20
20
20
20
21
21
21
21
22

iv

TABLE OF CONTENTS

.

.

.

.

.

.

.

.
.

.
.

.
.

.
.

.
.

2 Sorting
. . . . . . . . . . . . . . . .
.
Good sorting algorithms .
2.1
.
. . . . . . . . . . . . . . .
TeraSort .
.
.
.
.
2.2
Finding the winner and runner-up .
. . . . . . . . . . . . . . . .
2.3
Finding the min and max simultaneously . . . . . . . . . . . . .
2.4
.
. . . . . . . . . . . . . . .
.
Efﬁcient trials
.
.
2.5
. . . . . . . . . . . . . . .
.
.
.
Least distance sorting .
2.6
. . . . . . . . . . . . . . . .
.
Privacy and anonymization .
2.7
. . . . . . . . . . . . . . . .
.
.
Variable length sort .
2.8
. . . . . . . . . . . . . . . .
.
.
2.9
.
Unique elements .
. . . . . . . . . . . . . . .
.
.
.
2.10 Merging sorted arrays
. . . . . . . . . . . . . . . .
.
.
.
2.11 Approximate sort
.
. . . . . . . . . . . . . . . .
.
.
.
2.12 Running averages .
. . . . . . . . . . . . . . . .
.
.
.
2.13 Circuit simulation .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.

.
.

.
.

.
.

.

.

.

.

.

3 Meta-algorithms

.

.
.

.
.

.
.

.
.

.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.
Ties in a presidential election .
.
Red or blue house majority .
.
.
Load balancing .
.
Voltage selection .
.
.
Optimum buffer insertion .
.
.
Triangulation .
.
.
.
.
.
.
.
.
.
.

. . . . . . . . . . . . . . .
Longest nondecreasing subsequence .
3.1
. . . . . . . . . . . . . .
.
.
.
Frog crossing .
.
3.2
. . . . . . . . . . . . . .
.
.
.
Cutting paper .
3.3
.
. . . . . . . . . . . . . .
.
3.4 Word breaking .
.
.
3.5
. . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . .
3.6
.
. . . . . . . . . . . . . .
.
3.7
.
. . . . . . . . . . . . . . . .
3.8
.
. . . . . . . . . . . . . .
.
3.9
.
. . . . . . . . . . . . . .
.
3.10
.
.
.
3.11 Maximizing expressions .
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . .
.
.
Scheduling tutors .
3.12
.
. . . . . . . . . . . . . . . .
.
3.13 Minimize waiting time .
.
.
.
3.14 Huffman coding .
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . .
Efﬁcient user interface .
3.15
.
.
. . . . . . . . . . . . . . . .
Packing for USPS priority mail
3.16
. . . . . . . . . . . . . . . .
.
Points covering intervals .
3.17
. . . . . . . . . . . . . . . .
.
.
3.18 Rays covering arcs
. . . . . . . . . . . . . . . .
.
.
.
3.19
. . . . . . . . . . . . . .
.
.
.
.
3.20

k-clustering .
.
Party planning .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.

.

.

.

.

.

.

.

.

.

.

.

.

.

4 Algorithms on Graphs

4.1
4.2
4.3
4.4
4.5
4.6

.

.

.

.

.

.

.

.

.

.

Searching a maze .
. . . . . . . . . . . . . . .
.
Order nodes in a binary tree by depth . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
.
.
Connectedness .
. . . . . . . . . . . . . . .
.
PCB wiring .
.
.
. . . . . . . . . . . . . . .
.
Extended contacts .
. . . . . . . . . . . . . . .
.
.
Euler tour .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.

.

.

.

.

23
23
24
24
24
24
25
25
26
26
27
27
27
28

29
30
31
32
32
32
33
33
34
34
34
35
35
36
36
37
37
37
38
38
39

40
41
42
42
42
42
43

TABLE OF CONTENTS

.

.

.

.

.

.
.

.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

Ephemeral state in a ﬁnite state machine . . . . . . . . . . . . . .
4.7
.
.
. . . . . . . . . . . . . . . .
.
.
.
Tree diameter
4.8
. . . . . . . . . . . . . .
.
.
.
.
Timing analysis .
.
4.9
.
.
.
4.10
. . . . . . . . . . . . . .
.
Team photo day—1 .
. . . . . . . . . . . . . . . .
4.11 Assigning radio frequencies .
.
.
.
4.12
. . . . . . . . . . . . . . .
Shortest path with fewest edges .
. . . . . . . . . . . . . . . .
.
4.13 Counting shortest paths
4.14 Random directed acyclic graph .
. . . . . . . . . . . . . . . .
Shortest paths in the presence of randomization . . . . . . . . .
4.15
.
. . . . . . . . . . . . . . .
Traveling salesman with a choice .
4.16
. . . . . . . . . . . . . .
.
.
.
.
4.17 Road network .
.
. . . . . . . . . . . . . . . .
.
4.18
.
4.19 Arbitrage .
. . . . . . . . . . . . . .
.
.
.
Birkhoff-von Neumann decomposition . . . . . . . . . . . . . . .
4.20
.
. . . . . . . . . . . . . .
.
4.21 Channel capacity .
.
. . . . . . . . . . . . . .
.
4.22
.
.
.
.
4.23 Dancing with the stars .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
.
.
2-SAT .
4.24
.
. .
. . . . . . . . . . . . . . . .
.
.
Theory of equality .
4.25

.
Stable assignment .
.

.
Team photo day—2 .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.

.
.

.
.

.

.

.

.

.

.

.

.

.

5 Algorithms on Strings

5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9

Find all occurrences of a substring .
. . . . . . . . . . . . . . . .
String matching with unique characters . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . .
.
.
Rotate a string .
.
. . . . . . . . . . . . . . .
.
.
Test rotation .
.
.
.
.
Normalize URLs .
. . . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . . . .
Longest palindrome subsequence .
.
. . . . . . . . . . . . . . .
.
.
.
Pretty printing .
.
.
Edit distances .
. . . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . . . .
.
Regular expression matching .

.
.
.
.
.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.

.
.

.
.

.
.

.
.

.
.

.
.

6 Intractability

6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10

.

.

.

.

.

.

.

.

0-1 Knapsack .
.
.
.
Traveling salesman in the plane .
.
Facility location problem .
Computing xn .
.
.
.
.
.
.
CNF-SAT .
.
.
Scheduling .
.
.
.
.
Hardy-Ramanujan number
.
.
.
Collatz conjecture .
.
Nearest points in the plane .
.
.
Primality checking .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.

.

.

.

.

.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

.

.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
.
.
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
.

7 Parallel Computing

v

43
43
44
44
44
45
46
46
46
46
47
47
47
47
48
48
48
49
49

50
50
50
51
51
51
52
52
52
52

54
55
56
56
56
57
57
57
57
58
58

59

vi

TABLE OF CONTENTS

.

.

.

.

.

.
.

.
.

. .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
Servlet with caching .
7.1
.
.
Thread pools .
7.2
Asynchronous callbacks .
7.3
.
.
.
Timer .
.
.
7.4
Readers-writers .
.
.
.
7.5
Readers-writers with write preference .
7.6
Readers-writers with fairness .
7.7
.
Producer-consumer queue .
7.8
.
.
7.9
.
Barber shop .
.
.
7.10 Dining philosophers

. . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.
.

.
.

.
.

.
.

.
.

.

.

.

.

.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

8 Design Problems
.
.
.
.
8.1 Mosaic . .
.
.
.
Search engine .
8.2
.
.
.
IP forwarding .
8.3
.
.
.
.
Spell checker .
8.4
.
.
Stemming .
.
.
.
8.5
.
.
Distributed throttling .
8.6
.
.
Implement PageRank .
8.7
.
Scalable priority system .
8.8
8.9
.
.
Latency reduction .
8.10 Online advertising system .
.
8.11 Recommendation system .
.
.
.
8.12 Online poker .
.
.
.
.
8.13 Driving directions .
.
.
8.14
.
.
ISBN cache .
.
.
8.15 Distributing large ﬁles .
.
.
.
Leader election .
8.16
.
.
.
.
8.17 Host discovery .

.
.
.

.
.
.

.
.

.
.

.
.

.

.

.

.

.

.

.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.

. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.

.
.
.

.

.

9 Discrete Mathematics

.

.
.
.

.
.
.

.
.
.

9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
.
9.10 Hershey bar
.
9.11
9.12
.
9.13 m × n Chomp .

Computing the binomial coefﬁcients . . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . .
.
Climbing stairs
.
. . . . . . . . . . . . . . .
.
.
Ramsey theory .
.
.
.
500 doors .
. . . . . . . . . . . . . . .
.
.
.
. . . . . . . . . . . . . . . .
.
Height determination .
.
.
Betting on card colors .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
.
.
Even or odd .
.
.
Gassing up .
. . . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . . . .
.
Common knowledge .
. . . . . . . . . . . . . . .
.
.
.
.
.
.
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . . . .
.
.

.
n × n Chomp .
n × 2 Chomp .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.

.
.

60
60
61
61
61
62
62
62
63
63

64
64
65
65
65
65
66
66
66
67
67
67
67
68
68
68
68
69

70
70
71
71
71
71
71
73
73
73
74
74
74
74

TABLE OF CONTENTS

9.14
9.15
9.16

.
Picking up coins–I .
.
Picking up coins–II
Space-time intersections .

.
.

.
.

.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

10 Probability

.

.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.

.

.

.

.

.

.

.

.
.

.
.

.
.

.
.

.
.

.
.

.
.
.

.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
. . . . . . . . . . . . . . .
.
.
10.1 Ofﬂine sampling .
.
. . . . . . . . . . . . . . . .
.
.
10.2 Reservoir sampling .
. . . . . . . . . . . . . . .
.
.
10.3 Online sampling .
.
.
. . . . . . . . . . . . . . .
.
.
10.4 Random permutations—1 .
. . . . . . . . . . . . . . .
.
.
10.5 Random permutations—2 .
Forming a triangle from random lengths . . . . . . . . . . . . . .
10.6
.
.
10.7
. . . . . . . . . . . . . . .
.
Balls and bins .
. . . . . . . . . . . . . . . .
10.8 Random permutations .
.
10.9 Uniform random number generation .
. . . . . . . . . . . . . .
10.10 Nonuniform random number generation . . . . . . . . . . . . .
.
.
.
10.11 Expected number of dice rolls .
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . .
.
10.12 Option pricing—discrete case .
.
.
10.13 Option pricing with interest .
. . . . . . . . . . . . . . .
.
.
.
. . . . . . . . . . . . . . . .
10.14 Option pricing—continuous case .
.
.
. . . . . . . . . . . . . . .
.
.
.
10.15 Optimum bidding .
.
. . . . . . . . . . . . . . .
.
.
.
10.16 Once or twice .
.
.
.
. . . . . . . . . . . . . . .
.
.
10.17 Selecting a red card .
.
.
. . . . . . . . . . . . . . .
.
.
10.18 Selecting the best secretary .
. . . . . . . . . . . . . . .
10.19 Differentiating biases .
.
.
.
.
. . . . . . . . . . . . . . .
10.20 The complexity of AND-OR formulas

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.

.
.
.

.
.
.

.

.

.

.

.

11 Programming

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.
.

.
.
.
.

.
.
Basic coding questions .
11.1
.
11.2 Computing the parity of a long .
.
.
11.3 Reversing the bits in a long .
.
11.4 Run-length encoding .
.
.
Permuting the elements of an array .
11.5
11.6
.
Invert a permutation .
11.7 Reverse all the words in a sentence .
.
.
11.8 Reversing a singly linked list
.
.
.
11.9 Checking for cyclicity .
.
11.10 Deletion from a singly linked list
.
.
11.11 Binary search .
.
.
.
11.12 Programming languages .
.
.
.
11.13 Debugging and testing .
.
.
.
.
11.14 Best practices .
.
.
.
.
.
11.15 Operating systems
.
.
.
11.16 Tools
.
.
. .
.
.
.
11.17 Computer architecture .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.

.
.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .

.
.
.
.
.

vii

75
75
75

76
77
77
78
78
78
78
78
79
79
79
80
81
81
81
81
82
82
82
82
83

84
84
85
85
85
86
86
86
86
87
87
88
88
90
90
91
91
92

viii

TABLE OF CONTENTS

11.18 Systems . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. . . . . . . . . . . . . . . .

93

II The Interview

12 Strategies For A Great Interview
.

Presenting the solution .

.
.
Before the interview .
12.1
.
12.2 Approaching the problem .
.
.
12.3
.
.
.
12.4 Know your interviewers .
.
.
.
12.5 General conversation .
12.6 Other grandfatherly advice .

.

.

.

13 Conducting An Interview
.
.

.
.
13.1 Objective .
13.2 What to ask .
.
13.3 Conducting the interview .
.
Scoring and reporting .
13.4

.
.

.
.

.
.

.
.

.
.

.
.

.

.

.

.
.
.
.

III Solutions

1

2

Searching

Sorting

3 Meta-algorithms

4 Algorithms on Graphs

5 Algorithms on Strings

6

Intractability

7 Parallel Computing

8 Design Problems

9 Discrete Mathematics

10 Probability

11 Programming

Index of Problems

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.

.

. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
.

. . . . . . . . . . . . . . .
.
. . . . . . . . . . . . . . .
.
.
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .

94

95
95
95
97
97
98
98

100
100
101
102
102

104

105

118

124

137

148

152

159

166

177

184

195

200

